package com.foxit.pdf2html5.web.common;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;

public class HttpConnectionManager {

	private static HttpParams httpParams;

	private static PoolingClientConnectionManager cm;

	/**
	 * 最大连接数
	 */
	public final static int MAX_TOTAL_CONNECTIONS = 500;

	/**
	 * 获取连接的最大等待时间
	 */
	public final static int WAIT_TIMEOUT = 60000;

	/**
	 * 每个路由最大连接数
	 */
	public final static int MAX_ROUTE_CONNECTIONS = 300;

	/**
	 * 连接超时时间
	 */
	public final static int CONNECT_TIMEOUT = 10000;

	/**
	 * 读取超时时间
	 */
	public final static int READ_TIMEOUT = 10000;

	static {

		cm = new PoolingClientConnectionManager();
		cm.setMaxTotal(MAX_TOTAL_CONNECTIONS);
		cm.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS);

		HttpParams params = new BasicHttpParams();
		params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIMEOUT);
		params.setParameter(CoreConnectionPNames.SO_TIMEOUT, READ_TIMEOUT);

	}

	public static HttpClient getHttpClient() {
		return new DefaultHttpClient(cm, httpParams);
	}
	
	public static HttpPost getHttpPost(String url){
		HttpPost httpPost = new HttpPost(url);
		
		httpPost.setHeader("Connection", "keep-alive");
		httpPost.setHeader("Pragma", "no-cache");
		httpPost.setHeader("Cache-Control", "no-cache");
		
		return httpPost;
	}
}